package com.hwd.srb.core.controller.api;


import com.hwd.srb.common.result.R;
import com.hwd.srb.core.pojo.entity.Lend;
import com.hwd.srb.core.service.LendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标的准备表 前端控制器
 * </p>
 *
 * @author 黄伟东/Victor
 * @since 2022-03-10
 */
@RestController
@RequestMapping("/api/core/lend")
@Api(tags = "标的接口")
public class ApiLendController {

    @Resource
    private LendService lendService;

    /**
     * 计算收益
     *
     * @param invest       投资金额
     * @param yearRate     年华利率，如：12%
     * @param totalMonth   期数
     * @param returnMethod 还款方式
     * @return com.hwd.srb.common.result.R
     */
    @ApiParam(value = "计算收益")
    @GetMapping("/getInterestCount/{invest}/{yearRate}/{totalMonth}/{returnMethod}")
    public R getInterestCount(@ApiParam(value = "投资金额", required = true)
                              @PathVariable("invest") BigDecimal invest,

                              @ApiParam(value = "年华利率", required = true)
                              @PathVariable("yearRate") BigDecimal yearRate,

                              @ApiParam(value = "期数", required = true)
                              @PathVariable("totalMonth") Integer totalMonth,

                              @ApiParam(value = "还款方式", required = true)
                              @PathVariable("returnMethod") String returnMethod) {

        BigDecimal interestCount = lendService.getInterestCount(invest, yearRate, totalMonth, returnMethod);

        return R.ok().data("interestCount", interestCount);
    }

    /**
     * 获取标的列表
     *
     * @return com.hwd.srb.common.result.R
     */
    @ApiOperation("获取标的列表")
    @GetMapping("/list")
    public R list() {
        List<Lend> lendList = lendService.selectList();
        return R.ok().data("lendList", lendList);
    }

    /**
     * 根据标的id获取标的信息
     *
     * @param lendId 标的id
     * @return com.hwd.srb.common.result.R
     */
    @GetMapping("/show/{id}")
    public R show(@PathVariable("id") Long lendId) {

        Map<String, Object> lendDetail = lendService.getLendDetail(lendId);

        return R.ok().data("lendDetail", lendDetail);
    }

}

